﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Model;
using YouHP.Model.Group;
using YouHP.DALFactory;
using YouHP.Utility;

namespace YouHP.BLL
{
    public class GroupBLL
    {
        #region Singleton
        static GroupBLL _Instance;
        public static GroupBLL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new GroupBLL();
                }
                return _Instance;
            }
        }
        #endregion

        #region basic operations

        public bool AddGroup(GroupInfo info)
        {
            return DataAccess.GroupDAL.AddGroup(info);
        }

        public bool UpdateGroup(GroupInfo info)
        {
            return DataAccess.GroupDAL.UpdateGroup(info);
        }

        public bool DeleteGroup(int[] ids)
        {
            return DataAccess.GroupDAL.DeleteGroup(ids);
        }

        public bool InvalidateGroup(int groupid)
        {
            return DataAccess.GroupDAL.InvalidateGroup(groupid);
        }

#if GENDATA        
        public GroupInfo GetGroupInfo(int id)
        {
            GroupInfo info = new GroupInfo();
            info.Id = 10;
            info.GroupName = "测试圈子";
            info.Introduction = "圈子介绍";
            info.Logo = "/support/dnt/avatars/common/2.jpg";
            info.UserNum = 300;
            info.Role = 0;
            return info;
        }
#else
        public GroupInfo GetGroupInfo(int id)
        {
            return DataAccess.GroupDAL.GetGroupInfo(id);
        }

        public GroupInfo GetGroupInfoByName(string groupName)
        {
            return DataAccess.GroupDAL.GetGroupInfoByName(groupName);
        }

#endif
        // 取得圈子详细信息 （小圈子）
        public GroupInfo GetGroupByCreator(int groupId, int userId)
        {
            return new GroupInfo();
        }

        // 更新圈子的点击次数
        public bool IncreaseGroupHits(int groupId)
        {
            return DataAccess.GroupDAL.IncreaseGroupHits(groupId);
        }
        #endregion

        #region list and search operations

#if GENDATA
        public List<GroupInfo> GetGroupList(int pageSize, int pageIndex, out int count)
        {
            count = 100;
            List<GroupInfo> lst = new List<GroupInfo>();
            for (int i = 1; i <= pageSize; i++)
            {
                lst.Add(GetGroupInfo(i));
            }
            return lst;
        }
#else
        // 取得小圈子列表（小圈子）
        public List<GroupInfo> GetGroupList(int pageSize, int pageIndex, out int count)
        {
            return DataAccess.GroupDAL.GetGroupList(pageSize, pageIndex, out count);
        }
#endif
        // 取得所有小圈子列表（小圈子）
        public List<GroupInfo> GetAllGroupByName(string keyword)
        {
            int count = 0;
            return DataAccess.GroupDAL.GetSystemGroupList(Constants.maxPageSize, 1, out count);
        }

        //根据圈子类型取得圈子
        public List<GroupInfo> GetGroupListByType(int type,int pageSize, int pageIndex, out int count)
        {
            return DataAccess.GroupDAL.GetSystemGroupList(pageSize, pageIndex, type, out count);
        }

        //取得最新创建的n个圈子（小圈子）
        public List<GroupInfo> GetGroupListLatest(int n)
        {
            return DataAccess.GroupDAL.GetGroupListLatest(n);
        }

#if GENDATA        
        //取得最热的n个圈子 根据帖子数
        public List<GroupInfo> GetGroupListHotest(int n)
        {
            List<GroupInfo> lst = new List<GroupInfo>();
            for (int i = 1; i <= n; i++)
            {
                lst.Add(GetGroupInfo(i));
            }
            return lst;
        }
#else
        public List<GroupInfo> GetGroupListHotest(int n)
        {
            return DataAccess.GroupDAL.GetGroupListHotest(n);
        }
#endif

#if GENDATA
        //取得最热的n个圈子 根据帖子数
        public List<GroupInfo> GetGroupListCommend(int n)
        {
            List<GroupInfo> lst = new List<GroupInfo>();
            for (int i = 1; i <= n; i++)
            {
                lst.Add(GetGroupInfo(i));
            }
            return lst;
        }
#else
        //根据用户所患疾病推荐最热的n个圈子
        public List<GroupInfo> GetGroupListCommend(int pageSize, int pageIndex,GroupSearchCondition cond,out int groupCount)
        {
            return DataAccess.GroupDAL.GetGroupListBySearch(pageSize, pageIndex, cond, out groupCount);
        }
#endif

        // 取得某个用户创建的圈子
        public List<GroupInfo> GetGroupListByCreator(int pageSize, int pageIndex, int userId, out int groupCount)
        {
            return DataAccess.GroupDAL.GetGroupListByCreator(pageSize,pageIndex,userId,out groupCount);
        }

        // 取得某个用户加入的圈子(根据用户在圈子里的角色排序)
        public List<GroupInfo> GetGroupListByMember(int pageSize, int pageIndex, int userId, out int groupCount)
        {
            //return null;
            return DataAccess.GroupDAL.GetGroupListByUserId(pageSize, pageIndex, userId, out groupCount);
        }


#if GENDATA
         public List<GroupInfo> GetAllGroupsByUserId(int pageSize, int pageIndex, int userId, out int groupCount)
         {
             groupCount = 18;
             List<GroupInfo> lst = new List<GroupInfo>();
            for (int i = 1; i <= 18; i++)
            {
                lst.Add(GetGroupInfo(i));
            }
            return lst;
           
         }

#else 

        //取得某个用户所在的所有圈子
        public List<GroupInfo> GetAllGroupsByUserId(int pageSize, int pageIndex, int userId, out int groupCount)
        {
            return DataAccess.GroupDAL.GetAllGroupsByUserId(pageSize, pageIndex, userId, out groupCount);
        }
#endif
         //根据条件搜索圈子
        public List<GroupInfo> GetGroupListBySearch(int pageSize, int pageIndex, GroupSearchCondition cond, out int groupCount)
        {
            return DataAccess.GroupDAL.GetGroupListBySearch(pageSize, pageIndex, cond, out groupCount);

        }

    



        #endregion

#if GENDATA
        public List<GroupInfo> GetSystemGroupList(int pageSize, int pageIndex, out int dataCount)
        {
            List<GroupInfo> lst = new List<GroupInfo>();
            for (int i = 0; i < 8; i++)
            {
                GroupInfo info = new GroupInfo();
                lst.Add(info);
                info.Id = i * 10;
                info.GroupName = string.Format("圈子{0}", i);
                info.Introduction = "圈子介绍";
                for (int j = 0; j < 30; j++) info.Introduction += "圈子介绍";
            }
            dataCount = 23;
            return lst;
        }
#else 
        public List<GroupInfo> GetSystemGroupList(int pageSize, int pageIndex, out int dataCount)
        {
            return DataAccess.GroupDAL.GetSystemGroupList(pageSize, pageIndex, out dataCount);
        }
#endif

#if GENDATA
        /// <summary>
        /// 取得推荐圈子
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public GroupInfo GetRecommendedSystemGroup(int dszid)
        {
            GroupInfo info = new GroupInfo();
            info.Id = 1;
            info.GroupName = "推荐圈子";
            info.Introduction = "圈子介绍";
            return info;
        }

#else
        /// <summary>
        /// 取得推荐的疾病系统圈子
        /// </summary>
        /// <param name="dszid">疾病Id</param>
        public GroupInfo GetRecommendedSystemGroup(int dszid)
        {
            return DataAccess.GroupDAL.GetRecommendedSystemGroup(dszid);
        }

#endif

#if GENDATA        
        public List<TopicInfo> GetLattestTopics(int gid,int n)
        {
            List<TopicInfo> topiclist = new List<TopicInfo>(n);
            for (int i = 0; i < n; i++)
            {
                TopicInfo info = new TopicInfo();
                topiclist.Add(info);
                info.Id = i * 11 + 1;
                info.Title = "帖子主题";
                info.ReplyCount = 100;
                info.Hits = 100;

                info.PostUserId = 10;
                info.PostUserName = "发表人";
                info.PostTime = DateTime.Now.AddDays(-10);

                info.LatestReplyUserId = 120;
                info.LatestReplyUserName = "最后回复人";
                info.LatestReplyTime = DateTime.Now;
            }
            return topiclist;
        }
#else
        public List<TopicInfo> GetLattestTopics(int gid, int n)
        {
            throw new NotImplementedException();
        }
#endif


        //添加圈子邀请
        public bool AddGroupInvite(GroupInviteInfo info)
        {
            return DataAccess.GroupDAL.AddGroupInvite(info);
        }
        //圈子邀请列表
        public List<GroupInviteInfo> GetNewGroupInviteListByUserId(int pageSize, int pageIndex, int UserId, out int dataCount)
        {
            return DataAccess.GroupDAL.GetNewGroupInviteListByUserId(pageSize, pageIndex, UserId, out dataCount);
        }

        //删除圈子邀请（已查看）
        public bool DelGroupInvite(int id)
        {
            return DataAccess.GroupDAL.DelGroupInvite(id);
        }
    }
}